﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MLY.Reports.SCCoverReports
{
    public partial class MissingOrdersReport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GenerateReport();
            }
        }

        protected void DatesChanged(object sender, EventArgs e)
        {
            DatesPanel.Visible = (DatesLimitRBL.SelectedValue == "1");
        }

        protected void ReloadReport(object sender, EventArgs e)
        {
            GenerateReport();
        }

        private void GenerateReport()
        {
            string StartDate = "2000-01-01";
            string EndDate = "2030-01-01";
            string LimitDate = "0";

            string CustomerWOId = CustomerWONTextBox.Text.Trim();
            string ContractorWOId = ContractorWONTextBox.Text.Trim();

            // Checking dates:
            if (DatesLimitRBL.SelectedValue == "1")
            {
                StartDate = MLY.Utils.SqlDate(DateTime.Parse(txtFromDate.Text.Trim()));
                EndDate = MLY.Utils.SqlDate(DateTime.Parse(txtToDate.Text.Trim()));
                LimitDate = DateTypeDDL.SelectedValue;
            }

            ReportDataSource.SelectParameters.Clear(); 
            ReportDataSource.SelectParameters.Add("OrganizationId", SessParam.OrganizationId.ToString());
            
            ReportDataSource.SelectParameters.Add("LimitDate", LimitDate);
            ReportDataSource.SelectParameters.Add("StartDate", StartDate);
            ReportDataSource.SelectParameters.Add("EndDate", EndDate);

            if (ContractorWOId.Length > 0)
                ReportDataSource.SelectParameters.Add("ContractorWOId", ContractorWOId);  
            if (CustomerWOId.Length > 0)
                ReportDataSource.SelectParameters.Add("CustomerWOId", CustomerWOId);

            ReportDataSource.DataBind();
            ReportGridview.DataBind();
        }

        protected void ExportToExcel(object sender, EventArgs e)
        {
            MLY.Bll.ExcelExport.PrepareCsvFileFromGridview(ReportGridview, "LastOrdersReport");
        }
    }
}
